package ucl.cs.stockbroker.server;

/**
 * StockBroker Server Class.
 * @author  Min Park, Saniul Ahmed, Kamil Olesiejuk
 * @version 1.0
 */

import java.rmi.AccessException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;

import ucl.cs.stockbroker.common.StockBroker;

public class StockBrokerServer {
	public static void main(String args[]) {
		new StockBrokerServer();
	}

	public StockBrokerServer() {

		try {
			final StockBrokerImpl bank = new StockBrokerImpl();
			final StockBroker stub = (StockBroker) UnicastRemoteObject
					.exportObject(bank, 0);

			// Bind the UCLBank's stub in the RMI Registry
			// with the name 'StockBrokerServer' that clients can lookup.
			final Registry registry = LocateRegistry.getRegistry();
			registry.rebind("StockBrokerServer", stub);
		} catch (final AccessException e) {
			System.out.println("CRITICAL ERROR: Cannot bind service!");
			e.printStackTrace();
			System.exit(0);
		} catch (final RemoteException e) {
			System.out
					.println("CRITICAL ERROR: Cannot connect to the RMI Registry");
			e.printStackTrace();
			System.exit(1);
		}
		System.out.println("Server Started.");
	}
}
